package com.appos.service;

import com.appos.domain.ApplicationInfo;
import com.appos.domain.Book;
import com.appos.domain.WorkSheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class WorkSheetService {
    private static final Logger logger = LoggerFactory.getLogger(WorkSheetService.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存对象
     * @param workSheet
     * @return
     */
    public int save(WorkSheet workSheet) {
        workSheet.setCreateTime(new Date());
        workSheet.setUpdateTime(new Date());
        mongoTemplate.save(workSheet);
        return 1;
    }

    /**
     * 查询所有
     * @return
     */
    public List<WorkSheet> findAll() {
        return mongoTemplate.findAll(WorkSheet.class);
    }

    /***
     * 根据workSheetId查询
     * @param workSheetId
     * @return
     */
    public WorkSheet getWorkSheet(String workSheetId) {
        Query query = new Query(Criteria.where("workSheetId").is(workSheetId));
        return mongoTemplate.findOne(query, WorkSheet.class);
    }

    /***
     * 根据workSheetId查询
     * @param ids
     * @return
     */
    public List getWorkSheetList(List ids) {
        Query query = new Query(Criteria.where("workSheetId").in(ids));
        return mongoTemplate.find(query, WorkSheet.class);
    }

    /**
     * 更新对象
     *
     * @param workSheet
     * @return
     */
    public int updateWorkSheet(WorkSheet workSheet) {
        Query query = new Query(Criteria.where("workSheetId").is(workSheet.getWorkSheetId()));
        Update update = new Update().set("workSheetName", workSheet.getWorkSheetName())
                .set("widgetForm", workSheet.getWidgetForm()).set("updateTime",
                new Date());
        mongoTemplate.updateFirst(query, update, WorkSheet.class);
        return 1;
    }

    /***
     * 删除对象
     * @param applicationInfo
     * @return
     */
    public String deleteApp(ApplicationInfo applicationInfo) {
        mongoTemplate.remove(applicationInfo);
        return "success";
    }

    /***
     * 根据id查询
     * @param appId
     * @return
     */
    public ApplicationInfo getAppByAppId(String appId) {
        Query query = new Query(Criteria.where("appId").is(appId));
        return mongoTemplate.findOne(query, ApplicationInfo.class);
    }

    /**
     * 根据id删除
     *
     * @param appId
     * @return
     */
    public int deleteApplicationInfoByAppId(String appId) {
        // findOne
        ApplicationInfo applicationInfo = getAppByAppId(appId);
        // delete
        deleteApp(applicationInfo);
        return 1;
    }

    /**
     * 模糊查询
     * @param search
     * @return
     */
    public List<Book> findByLikes(String search){
        Query query = new Query();
        Criteria criteria = new Criteria();
        //criteria.where("name").regex(search);
        Pattern pattern = Pattern.compile("^.*" + search + ".*$" , Pattern.CASE_INSENSITIVE);
        criteria.where("name").regex(pattern);
        List<Book> lists = mongoTemplate.findAllAndRemove(query, Book.class);
        return lists;
    }
}
